﻿namespace MySql.Data.Common
{
    using System;
    using System.Collections.Specialized;
    using System.Reflection;

    internal class Cache : NameObjectCollectionBase
    {
        private int capacity;

        public Cache(int initialCapacity, int capacity) : base(initialCapacity)
        {
            this.capacity = capacity;
        }

        public void Add(string key, object value)
        {
            this.InternalAdd(key, value);
        }

        private void InternalAdd(string key, object value)
        {
            if (base.Count == this.capacity)
            {
                this.RemoveOldestItem();
            }
            base.BaseAdd(key, value);
        }

        private void RemoveOldestItem()
        {
            base.BaseRemoveAt(0);
        }

        public object this[string key]
        {
            get
            {
                return base.BaseGet(key);
            }
            set
            {
                this.InternalAdd(key, value);
            }
        }
    }
}

